import routes from '@/config/routes';
import { isEmpty } from 'lodash-es'

/**
  * 打平路由配置列表 避免多循环嵌套
  * @items 
  */
export const getFlatMenuList = (items:any[]) => {
  let list = {}
  for (let i = 0; i < items.length; i++) {
    const item = items?.[i]
    if (item?.path) {
      list[item?.path] = {
        ...item,
      }

    }
    if (item?.children?.length) {
      const children = getFlatMenuList(item?.children)
      if (!isEmpty(children)) {
        list = { ...list, ...children }
      }
    }
  }
  return list
}
const flatRoutes = getFlatMenuList(routes)


export function toTree<T>(items: Array<any>, parentId = 0): Array<any>{
  let list=[]
  for (let i = 0; i < items?.length; i++) {
    const item = items?.[i];
      if (item?.parentId === parentId ) {
        const newItem = {
          ...item,
          title: item?.name,
          key: item?.id,
          label:item?.name,
        }
        const children = toTree(items, item?.id)
        if (children?.length) {
          newItem.children=children
        }
        list.push(newItem)
      }
   
  
  }
  return list
}